// scroller.js


// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// 
// Coded by Travis Beckham
// http://www.squidfingers.com | http://www.podlob.com
// If want to use this code, feel free to do so, but please leave this message intact.
//
// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// --- version date: 01/24/03 ---------------------------------------------------------

// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Cross-Browser Functions

var dom = document.getElementById;
var iex = document.all;
var ns4 = document.layers;

function addScrollEvent(event,method){
	this[event] = method;
	if(ns4) this.captureEvents(Event[event.substr(2,event.length).toUpperCase()]);
}
function removeScrollEvent(event){
	this[event] = null;
	if(ns4) this.releaseEvents(Event[event.substr(2,event.length).toUpperCase()]);
}
function getElement(name,nest){
	nest = nest ? "document."+nest+"." : "";
	var el = dom ? document.getElementById(name) : iex ? document.all[name] : ns4 ? eval(nest+"document."+name) : false;
	
	el.css = ns4 ? el : el.style;
	el.getLeft = function(){return parseInt(el.css.left) || 0};
	el.setLeft = function(x){el.css.left = ns4 ? x: x+"px"};
	el.getWidth = function(){return ns4 ? el.document.width : el.offsetWidth};
	el.getClipWidth = function(){return ns4 ? el.clip.width : el.offsetWidth};
	el.hideVis = function(){el.css.visibility="hidden"};
	el.addScrollEvent = addScrollEvent;
	el.removeScrollEvent = removeScrollEvent;
	return el;
}
function getXMouse(e){
	return iex ? event.clientX : e.pageX;
}

document.addScrollEvent = addScrollEvent;
document.removeScrollEvent = removeScrollEvent;

// ||||||||||||||||||||||||||||||||||||||||||||||||||
// Scroller Class
var noScroll;
ScrollObj = function(speed, dragWidth, trackWidth, trackObj, upObj, downObj, dragObj, contentMaskObj, contentObj){
	this.speed = speed;
	this.dragWidth = dragWidth;
	this.trackWidth = trackWidth;
	this.trackObj = getElement(trackObj);
	this.upObj = getElement(upObj);
	this.downObj = getElement(downObj);
	this.dragObj = getElement(dragObj);
	this.contentMaskObj = getElement(contentMaskObj);
	this.contentObj = getElement(contentObj,contentMaskObj);
	this.obj = contentObj+"Object";
	eval(this.obj+"=this");
	
	this.trackLeft= this.dragObj.getLeft();
	this.trackLength = this.trackWidth-this.dragWidth;
	this.trackRight = this.trackLeft+this.trackLength;
	this.contentMaskWidth = this.contentMaskObj.getClipWidth();
	this.contentWidth = this.contentObj.getWidth();
	this.contentLength = this.contentWidth-this.contentMaskWidth;
	this.scrollLength = this.trackLength/this.contentLength;
	this.scrollTimer = null;
	
	if(this.contentWidth <= this.contentMaskWidth){
		this.dragObj.hideVis();
		noScroll = true;
	}else{
		var self = this;
		this.trackObj.addScrollEvent("onmousedown", function(e){self.scrollJump(e);return false});
		
		this.upObj.addScrollEvent("onmousedown", function(){self.scroll(self.speed);return false});
		this.upObj.addScrollEvent("onmouseup", function(){self.stopScroll()});
		this.upObj.addScrollEvent("onmouseout", function(){self.stopScroll()});
		
		this.downObj.addScrollEvent("onmousedown", function(){self.scroll(-self.speed);return false});
		this.downObj.addScrollEvent("onmouseup", function(){self.stopScroll()});
		this.downObj.addScrollEvent("onmouseout", function(){self.stopScroll()});
		
		this.dragObj.addScrollEvent("onmousedown", function(e){self.startDrag(e);return false});
		if(iex) this.dragObj.addScrollEvent("ondragstart", function(){return false});
	}
	
}


//if (window.addEventListener)
    /** DOMMouseScroll is for mozilla. */
   // window.addEventListener('DOMMouseScroll', wheel, false);
	/** IE/Opera. */
	//window.onmousewheel = document.onmousewheel = wheel;

function handle(delta) {
        if(! noScroll){
	        if (delta < 0){
				myScroll.scrollWheel('right')
			} else {
				myScroll.scrollWheel('left')
			}
		}
}


function wheel(event){
       var delta = 0;
        if (!event) /* For IE. */
                event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta/120;
                /** In Opera 9, delta differs in sign as compared to IE.
                 */
                if (window.opera)
                        delta = -delta;
        } else if (event.detail) { /** Mozilla case. */
                /** In Mozilla, sign of delta is different than in IE.
                 * Also, delta is multiple of 3.
                 */
                delta = -event.detail/3;
        }
        /** If delta is nonzero, handle it.
         * Basically, delta is now positive if wheel was scrolled up,
         * and negative, if wheel was scrolled down.
         */
        if (delta)
                handle(delta);
        /** Prevent default actions caused by mouse wheel.
         * That might be ugly, but we handle scrolls somehow
         * anyway, so don't bother here..
         */
        if (event.preventDefault)
                event.preventDefault();
		event.returnValue = false;

}

ScrollObj.prototype.startDrag = function(e){
	this.dragStartMouse = getXMouse(e);
	this.dragStartOffset = this.dragObj.getLeft();
	var self = this;
	document.addScrollEvent("onmousemove", function(e){self.drag(e)});
	document.addScrollEvent("onmouseup", function(){self.stopDrag()});
}
ScrollObj.prototype.stopDrag = function(){
	document.removeScrollEvent("onmousemove");
	document.removeScrollEvent("onmouseup");
}
ScrollObj.prototype.drag = function(e){
	var currentMouse = getXMouse(e);
	var mouseDifference = currentMouse-this.dragStartMouse;
	var dragDistance = this.dragStartOffset+mouseDifference;
	var dragMovement = (dragDistance<this.trackLeft) ? this.trackLeft : (dragDistance>this.trackRight) ? this.trackRight : dragDistance;
	this.dragObj.setLeft(dragMovement);
	var contentMovement = -(dragMovement-this.trackLeft)*(1/this.scrollLength);
	this.contentObj.setLeft(contentMovement);
}
ScrollObj.prototype.scroll = function(speed){
	var contentMovement = this.contentObj.getLeft()+speed;
	var dragMovement = this.trackLeft-Math.round(this.contentObj.getLeft()*(this.trackLength/this.contentLength));

	if(contentMovement > 0){
		contentMovement = 0;
	}else if(contentMovement < -this.contentLength){
		contentMovement = -this.contentLength;
	}
	if(dragMovement < this.trackLeft){
		dragMovement = this.trackLeft;
	}else if(dragMovement > this.trackRight){
		dragMovement = this.trackRight;
	}
		
	this.contentObj.setLeft(contentMovement);

	this.dragObj.setLeft(dragMovement);
	this.scrollTimer = window.setTimeout(this.obj+".scroll("+speed+")",25);

}
ScrollObj.prototype.stopScroll = function(){
	if(this.scrollTimer){
		window.clearTimeout(this.scrollTimer);
		this.scrollTimer = null;
	}
}
ScrollObj.prototype.scrollJump = function(e){
	var currentMouse = getXMouse(e);

	var deptCtn = (document.getElementById('deptScroller').offsetLeft) + 40;
	
	currentMouse = currentMouse - deptCtn;
	var dragDistance = currentMouse-(this.dragWidth/2);
	var dragMovement = (dragDistance<this.trackLeft) ? this.trackLeft : (dragDistance>this.trackRight) ? this.trackRight : dragDistance;
	this.dragObj.setLeft(dragMovement);
	var contentMovement = -(dragMovement-this.trackLeft)*(1/this.scrollLength);
	this.contentObj.setLeft(contentMovement);
}

var wheelLoc = 230;

ScrollObj.prototype.scrollWheel = function(dir){
	if (dir == 'right'){
		var dragMovement = wheelLoc + 15;
		
		this.dragObj.setLeft(dragMovement);
		
		var contentMovement = -(dragMovement-this.trackLeft + 16)*(1/this.scrollLength);
	
		if(dragMovement<530){
			this.contentObj.setLeft(contentMovement);
			wheelLoc = wheelLoc + 15;
		}
	} else {
		var dragMovement = wheelLoc - 15;
		
		this.dragObj.setLeft(dragMovement);
		
		var contentMovement = -(dragMovement-this.trackLeft-15)*(1/this.scrollLength);
	
		if(dragMovement>230){
			this.contentObj.setLeft(contentMovement);
			wheelLoc = wheelLoc - 15;
		}
	}	
}

// ||||||||||||||||||||||||||||||||||||||||||||||||||
// Misc Functions

function fixNetscape4(){
	if(ns4origWidth != window.innerWidth || ns4origHeight != window.innerHeight){
		window.location.reload();
	}	
}
if(document.layers){
	ns4origWidth = window.innerWidth;
	ns4origHeight = window.innerHeight;
	window.onresize = fixNetscape4;
}

// ||||||||||||||||||||||||||||||||||||||||||||||||||

